package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.DirectInvestmentFundEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class DirectInvestmentFundDetailProcess implements PageProcessor{

	private static Logger logger = Logger.getLogger(DirectInvestmentFundDetailProcess.class);
	
	private static int record = 0;
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		record++;
		logger.info("正在解析第"+record+"条数据:");
		Document doc = Jsoup.parse(page.getContent());
		JSONObject ctxObj = (JSONObject)page.getRequest().getExtParams().get("ctxObj");
		DirectInvestmentFundEntity dif = new DirectInvestmentFundEntity();
		dif.setCreateTime(new Timestamp(System.currentTimeMillis()));
		String url = page.getRequest().getUrl();
		dif.setUrl(url);
		Element table = doc.select("table[class=\"table table-center table-info\"] > tbody").first();
		dif.setName(table.getElementsMatchingOwnText("产品名称").first().nextElementSibling().text());
		dif.setCode(table.getElementsMatchingOwnText("产品编码").first().nextElementSibling().text());
		dif.setAoinName(table.getElementsMatchingOwnText("直投子公司名称").first().nextElementSibling().text());
		dif.setManagerName(table.getElementsMatchingOwnText("管理机构名称").first().nextElementSibling().text());
		String createDate = table.getElementsMatchingOwnText("设立日期").first().nextElementSibling().text();
		if(StringUtils.isNotEmpty(createDate)){
			dif.setCreateDate(DateUtils.parseDate(createDate));
		}
		String putonRecordDate = table.getElementsMatchingOwnText("备案日期").first().nextElementSibling().text();
		if(StringUtils.isNotEmpty(putonRecordDate)){
			dif.setPutonRecordDate(DateUtils.parseDate(putonRecordDate));
		}
		dif.setFundType(table.getElementsMatchingOwnText("基金类型").first().nextElementSibling().text());
		dif.setOrganizeType(table.getElementsMatchingOwnText("组织形式").first().nextElementSibling().text());
		dif.setWorkingState(table.getElementsMatchingOwnText("运作状态").first().nextElementSibling().text());
		dif.setIsDeputeManage(table.getElementsMatchingOwnText("是否托管").first().nextElementSibling().text());
		dif.setCustodian(table.getElementsMatchingOwnText("托管人名称").first().nextElementSibling().text());
		if(!"null".equals(ctxObj.getString("buyMoney"))){
			dif.setBuyMoney(Double.valueOf(ctxObj.getString("buyMoney")));
		}
		dif.setId(ctxObj.getString("id"));
		if(!"null".equals(ctxObj.getString("mandatorName"))){
			dif.setMandatorName(ctxObj.getString("mandatorName"));
		}
		if( !"null".equals(ctxObj.getString("scope"))){
			dif.setScope(ctxObj.getString("scope"));
		}
		if(!"null".equals(ctxObj.getString("submitDate"))){
			dif.setSubmitDate(DateUtils.parseDate(ctxObj.getString("submitDate")));
		}
		dif.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(dif);
		objectContainer.add(dif);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
